<?php
const AES_KEY = "asdgagerasdcveverfggqerfasdf";

// 应用公共文件
function success($data = [])
{
    $return = [
        'code' => 200, 'msg' => '操作成功'
    ];
    if($data){
        $return = array_merge($return, $data);
    }
    return $return;
}

//操作失败
function error($code, $msg, $data = [])
{
    return ['code' => $code, 'msg' => $msg, 'data' => $data];
}


function get_request_ip(){
    return request()->ip();
}

function now_time(){
    return time();
}

function now_date(){
    return date('Y-m-d H:i:s');
}

/**
 * 加密方法
 * @param string $str
 * @return string
 */
function aes_encrypt($ciphertext, $key = '')
{
    if ($key == '') {
        $key = AES_KEY;
    }

    $iv = substr($key, 0, 16);

    return base64_encode(openssl_encrypt($ciphertext, 'AES-256-CBC', $key, OPENSSL_RAW_DATA, $iv));
}

/**
 * 解密方法
 * @param string $str
 * @return string
 */
function aes_decrypt($ciphertext, $key = '')
{
    if ($key == '') {
        $key = AES_KEY;
    }
    $iv = substr($key, 0, 16);

    return openssl_decrypt(base64_decode($ciphertext), 'AES-256-CBC', $key, OPENSSL_RAW_DATA, $iv);
}